package linker.plugin;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class GDocsList extends PluginItem implements ActionListener {
	// @_@
	private static final long serialVersionUID = 1L;

	private static final String DEF_FEED_HOST = "docs.google.com";

	private static final String FEED_URL_PATH = "/feeds/documents/private/full";

	private String userName = null;

	private String password = null;

	private JButton refreshButton = new JButton("Refresh");

	private JButton uploadButton = new JButton("Upload");

	private DefaultListModel listModel = new DefaultListModel();

	private DocsService service = null;

	private URL feedUrl;

	public GDocsList() {
		super("GDocsList");
		setName("GDocsList");
		try {
			feedUrl = new URL("http://" + DEF_FEED_HOST + FEED_URL_PATH);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public String toString() {
		return "GDocsList";
	}

	public void refresh() {

		if (userName == null || password == null) {
			userName = JOptionPane.showInputDialog("Input userName");
			password = JOptionPane.showInputDialog("Input password");
		}

		if (service == null) {
			service = new DocsService("LinkerGDocsList");

			try {
				service.setUserCredentials(userName, password);

			} catch (AuthenticationException e) {
				e.printStackTrace();
			}
		}

		try {
			DocumentListFeed feed = service.getFeed(feedUrl,
					DocumentListFeed.class);

			for (DocumentListEntry entry : feed.getEntries()) {
				listModel.addElement(entry.getTitle().getPlainText());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	public void create() {
		Container container = getContentPane();

		JPanel userPanel = new JPanel();
		uploadButton.addActionListener(this);
		userPanel.add(uploadButton);
		refreshButton.addActionListener(this);
		userPanel.add(refreshButton);
		container.add(userPanel, BorderLayout.NORTH);

		JList list = new JList();
		list.setModel(listModel);
		container.add(new JScrollPane(list), BorderLayout.CENTER);

		setWindow();
	}

	public void setPluginDir(String dir) {
		icon = new ImageIcon(dir + fileSeparator + "icon.png");

		create();
	}

	public static void main(String args[]) {
		GDocsList docsList = new GDocsList();
		docsList
				.setPluginDir("/home/cox/Desktop/linker/resources/plugins/GDocsList");
		docsList.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == refreshButton) {
			refresh();
		} else if (e.getSource() == uploadButton) {
			uploadFile();
		}
	}

	private void uploadFile() {
		JFileChooser chooser = new JFileChooser();
		chooser.showOpenDialog(this);
		File file = chooser.getSelectedFile();

		System.out.println(file.getAbsolutePath());

	}

}
